package com.miku.ai.scoring;

import com.miku.ai.common.ErrorCode;
import com.miku.ai.exception.BusinessException;
import com.miku.ai.model.entity.App;
import com.miku.ai.model.entity.UserAnswer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 评分策略执行器
 */
@Service
public class ScoringStrategyExecutor {

    //策略列表
    @Resource
    private List<ScoringStrategy> scoringStrategyList;


    public UserAnswer doScore(List<String> choiceList, App app) throws Exception {
        Integer appType = app.getAppType();
        Integer scoringStrategy = app.getScoringStrategy();
        if (appType == null || scoringStrategy == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "应用类型或评分策略不能为空");
        }

        //根据注解获取策略
        // 遍历scoringStrategyList中的每一个ScoringStrategy
        for (ScoringStrategy strategy : scoringStrategyList) {
            // 判断strategy的类是否被ScoringSrategyConfig注解修饰
            if (strategy.getClass().isAnnotationPresent(ScoringSrategyConfig.class)) {
                // 获取strategy的类上的ScoringSrategyConfig注解
                ScoringSrategyConfig scoringSrategyConfig = strategy.getClass().getAnnotation(ScoringSrategyConfig.class);
                // 判断ScoringSrategyConfig注解中的appType和scoringStrategy是否与传入的appType和scoringStrategy相等
                if(scoringSrategyConfig.appType()==appType && scoringSrategyConfig.scoringStrategy()==scoringStrategy){
                    return strategy.doScore(choiceList,app);
                }
            }
        }
        throw new BusinessException(ErrorCode.SYSTEM_ERROR, "应用配置有误，未找到匹配的策略");
    }

}
